//
//  HomeViewController.m
//  Docter
//
//  Created by liuzhishan on 15/12/14.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "HomeViewController.h"
#import "ZHHospitalModel.h"
#import "ZHHomeCollectionViewCell.h"
#import "ZHHomeTableViewCell.h"
#import "ZHHospitalMainViewController.h"
#import "ZHLoginViewController.h"
#import "ZHBackAlertView.h"

@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UIImageView *imageLogo;

@property (nonatomic,retain) UIButton *btnChange;

@property (nonatomic,retain) UIButton *btnChooseAll;

@property (nonatomic,retain) NSMutableArray *dataArray;
@property (nonatomic,retain) UICollectionView *myColletionView;
@property (nonatomic,retain) UITableView *myTableView;

@property (nonatomic,assign) BOOL tableViewUp;

@property (nonatomic,strong) UIAlertView *alertView;

@property (nonatomic,strong)  ZHBackAlertView * back;

@end

@implementation HomeViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
  [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:0];

    
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.tableViewUp = NO;
    
    [self addMyBgViews];
    
    [self addMyTableView];

    [self addMyColletionView];
 
    
}

-(void)trueCancel
{
    
    [[NSUserDefaults standardUserDefaults] setValue:@"no" forKey:@"loginTrue"];
    
    
    ZHLoginViewController *login = [[ZHLoginViewController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = login;
    

    
}

-(void)CancelAction
{

    if (self.back == nil) {
        
      self.back=[[ZHBackAlertView alloc]init];
        [self.back setMyFramewithX:700 andY:55 andWidth:250 andHeight:161];
        [self.view addSubview:self.back];
        
        [self.back.buttonYes addTarget:self action:@selector(trueCancel) forControlEvents:UIControlEventTouchUpInside];
        
        [self.back.buttonNo addTarget:self action:@selector(clinicCancel) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    self.back.hidden = NO;

    
 }

-(void)clinicCancel
{
    
    self.back.hidden = YES;
    
}

-(void)addMyBgViews
{
    self.naviTitleLable.text = @"主页";
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setMyFramewithX:900 andY:25 andWidth:75 andHeight:30];
    [btn setTitle:@"退出登录" forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn.titleLabel.font = [UIFont systemFontOfSize: 18*WIDPro - 2];
    [btn addTarget:self action:@selector(CancelAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:btn];

    self.imageLogo = [[UIImageView alloc]init];
    self.imageLogo.image = [UIImage imageNamed:@"home-4"];
    [self.imageLogo setMyFramewithX:35 andY:630 andWidth:241 andHeight:67];
    [self.view addSubview:self.imageLogo];
    
    
    self.btnChange = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnChange setMyFramewithX:850 andY:123 andWidth:74 andHeight:48];
    [self.btnChange setBackgroundImage:[UIImage imageNamed:@"home-1"] forState:UIControlStateNormal];
    [self.view addSubview:self.btnChange];
    
    [self.btnChange addTarget:self action:@selector(changeViewController) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.btnChooseAll = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnChooseAll setMyFramewithX:798 andY:608 andWidth:173 andHeight:35];
    [self.btnChooseAll setBackgroundImage:[UIImage imageNamed:@"home-3"] forState:UIControlStateNormal];
    [self.view addSubview:self.btnChooseAll];
    
    [self.btnChooseAll addTarget:self action:@selector(selectAllMyChoose) forControlEvents:(UIControlEventTouchUpInside)];
    
}
-(NSString *)getTodayDate
{
    return @"2015-11-16";
}

-(void)selectAllMyChoose
{
    
    [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"judgeAll"];
    
    ZHHospitalMainViewController *main = [[ZHHospitalMainViewController alloc]init];
    main.isAll = YES;

    [self.navigationController pushViewController:main animated:YES];

    
}

-(void)changeViewController
{
    if (self.tableViewUp) {
       
        [self.myTableView removeFromSuperview];
        [self.view addSubview:self.myColletionView];

        
         [self.view bringSubviewToFront:self.myColletionView];
        
         [self.btnChange setBackgroundImage:[UIImage imageNamed:@"home-1"] forState:UIControlStateNormal];
        
    }
    
    else
    {
        [self.myColletionView removeFromSuperview];
        [self.view addSubview:self.myTableView];
        
        
        [self.btnChange setBackgroundImage:[UIImage imageNamed:@"table-1"] forState:UIControlStateNormal];

        
    }
    
    self.tableViewUp = !self.tableViewUp;
    
   
}



#pragma mark 添加TableView
-(void)addMyTableView
{
    self.myTableView = [[UITableView alloc]init];
    [self.myTableView setMyFramewithX:35 andY:109 andWidth:667 andHeight:490];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.myTableView.backgroundColor = [UIColor clearColor];
    
    //[self.view addSubview:self.myTableView];
    

}

#pragma mark 添加ColletionView
-(void)addMyColletionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(WIDPro  * 120 , WIDPro *  152);
    
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 20;
    
    // flowLayout.headerReferenceSize = CGSizeMake(WIDTH, HEIGHT * 0.07);
    flowLayout.sectionInset = UIEdgeInsetsMake(30*HEIPro,35*WIDPro, 0, 35*WIDPro);
    
    
    _myColletionView = [[UICollectionView alloc] initWithFrame: CGRectMake(35*WIDPro,109*HEIPro, 613*WIDPro, HEIPro*504)collectionViewLayout:flowLayout];
    _myColletionView.bounces = NO;
    _myColletionView.delegate = self;
    _myColletionView.dataSource = self;
    
    _myColletionView.backgroundColor = [UIColor clearColor];
    
    [_myColletionView registerClass:[ZHHomeCollectionViewCell class] forCellWithReuseIdentifier:@"HospitalCell"];
    
    [self.view addSubview:_myColletionView];
    
    
   
    [self getDataWithHomeUrl];
}


#pragma mark 请求数据
-(void)getDataWithHomeUrl
{
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",HOMEURL,[[NSUserDefaults standardUserDefaults] objectForKey:@"userMessage"]];
    

    NSString *pathStr =    [ZHSaveFilePath getHomeDirectoriesInDomainsPath];
    
    [AFNData getAFNDataWithUrl:urlStr success:^(id responseObject) {
        
  
        NSDictionary *dic = responseObject;
        
        [dic writeToFile:pathStr atomically:YES];

        [self reloadDataWith:dic];
        
        
    } failure:^(NSError *error) {
        
        
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:pathStr];
        [self reloadDataWith:dic];
        
    }];
}


-(void)reloadDataWith:(NSDictionary *)dic
{
    NSArray *array = [dic objectForKey:@"payload"];
    
    self.dataArray = [NSMutableArray arrayWithArray:array];
    
    NSInteger count = self.dataArray.count;
    
    for (NSInteger i = 0; i < count; i ++) {
        
        NSDictionary *dic1 = [self.dataArray objectAtIndex:i];
        
        ZHHospitalModel *model = [[ZHHospitalModel alloc]init];
        
        [model setValuesForKeysWithDictionary:dic1];
        model.shopIdentity = [[dic1 objectForKey:@"shopIdentity"] integerValue];
        
        [self.dataArray replaceObjectAtIndex:i withObject:model];
        
    }
    
    [self.myColletionView reloadData];
    [self.myTableView reloadData];

    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZHHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HospitalCell" forIndexPath:indexPath];
    
    ZHHospitalModel *model = [self.dataArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    return cell;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self pushMySelectController:indexPath.row];
    
    
}

-(void)pushMySelectController:(NSInteger)index
{
    
    [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"judgeAll"];
    ZHHospitalMainViewController *main = [[ZHHospitalMainViewController alloc]init];
    
    ZHHospitalModel *model =  [self.dataArray objectAtIndex:index];
    
     main.hosModel = model;
    
    [self.navigationController pushViewController:main animated:YES];
    
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return  self.dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"hospitalTableCell";
    
    ZHHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        
        cell = [[ZHHomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    
    ZHHospitalModel *model = [self.dataArray objectAtIndex:indexPath.row];
    
    
    cell.model = model;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  120*HEIPro;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
     [self pushMySelectController:indexPath.row];
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}

 

@end
